/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number[][]}
 */
var pathSum = function (root, targetSum) {
    if (root === null) return []
    let res = []
    getPath(root, targetSum, 0, [], res)
    return res
};

function getPath(root, targetSum, sum, data, res) {
    data.push(root.val)
    sum += root.val

    if (root.left === null && root.right === null && sum === targetSum) {
        res.push([...data])
    }

    root.left && getPath(root.left, targetSum, sum, data, res)
    root.right && getPath(root.right, targetSum, sum, data, res)

    data.pop()
    return
}